package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.entity.Schedule;
import com.example.football.util.JDBCUtils;
import com.example.football.util.SetTeamlogo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AllscheduleActivity extends AppCompatActivity {

    public String league;
    TextView yingchao;
    TextView xijia;
    TextView fajia;
    TextView yijia;
    TextView dejia;
    ListView myList;
    static public ArrayList<Schedule> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allschedule);


        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        list= new ArrayList<>();
        Intent intent = getIntent();
        league = intent.getStringExtra("league");

        yingchao = findViewById(R.id.yingchao);
        yijia = findViewById(R.id.yijia);
        fajia = findViewById(R.id.fajia);
        dejia = findViewById(R.id.dejia);
        xijia = findViewById(R.id.xijia);

        if(league.equals("英超")){
            yingchao.setTextColor(getResources().getColor(R.color.red));
        }else  if(league.equals("西甲")){
            xijia.setTextColor(getResources().getColor(R.color.red));
        }else  if(league.equals("意甲")){
            yijia.setTextColor(getResources().getColor(R.color.red));
        }else  if(league.equals("德甲")){
            dejia.setTextColor(getResources().getColor(R.color.red));
        }else  if(league.equals("法甲")){
            fajia.setTextColor(getResources().getColor(R.color.red));
        }

        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("league",league);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPfindschedulebyleague";
        RequestQueue requestQueue= Volley.newRequestQueue(AllscheduleActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
//                    Log.d("信息", jsonObject.toString());
//                    String msg = jsonObject.getString("msg");
//                    Log.d("msg", msg);
                    JSONArray detail = jsonObject.getJSONArray("detail");
                    for(int i = 0; i < detail.length(); i++){
                        JSONObject data = detail.getJSONObject(i);
                        String s = data.getString("time").substring(0,10)+" "+data.getString("time").substring(11,19);
                        list.add(new Schedule(data.getInt("id"),data.getString("hometeam"),data.getString("guestteam"),
                                    s,data.getString("platform"),data.getString("league")));
                    }
                    myList = findViewById(R.id.mylist8);
                    final MyAdapter myAdapter = new MyAdapter();
                    myList.setAdapter(myAdapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(AllscheduleActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);

    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {//获取item的数量
            return list.size();
        }

        @Override
        public Object getItem(int i) {//i表示当前item的序号，序号从0开始编写，getIte用于获取某个item
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {//获取选中的item的编号
            return i;
        }

        @Override//用于配置listview要加载的内容，包括视图和数据
        public View getView(final int i, View view, ViewGroup viewGroup) {

//             优化写法
            ViewHolder holder = null;
            if(view == null){
                //这里是将item视图与view绑定起来
                view = View.inflate(AllscheduleActivity.this,R.layout.schedule_item,null);
                holder = new ViewHolder();
//                注意，这里是去view的视图里面拿id
                holder.hometeam = view.findViewById(R.id.hometeam);
                holder.guestteam = view.findViewById(R.id.guestteam);
                holder.time = view.findViewById(R.id.time);
                holder.league = view.findViewById(R.id.league);
                holder.platform = view.findViewById(R.id.platform);
                holder.hometeamlogo = view.findViewById(R.id.hometeamlogo);
                holder.guestteamlogo = view.findViewById(R.id.guestteamlogo);
                holder.yuding = view.findViewById(R.id.yuding);

                holder.hometeam.setText(list.get(i).getHometeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());
                holder.guestteam.setText(list.get(i).getGuestteam());
                holder.time.setText(list.get(i).getTime());
                holder.league.setText(list.get(i).getLeague());
                holder.platform.setText(list.get(i).getPlatform());
                SetTeamlogo.setTeamlogo(holder.hometeamlogo,list.get(i).getHometeam());
                SetTeamlogo.setTeamlogo(holder.guestteamlogo,list.get(i).getGuestteam());

                view.setTag(holder);
            }else {
                holder = (ViewHolder) view.getTag();
                holder.hometeam.setText(list.get(i).getHometeam());
//                holder.iv.setBackgroundResource(foods.get(i).getIcon());
                holder.guestteam.setText(list.get(i).getGuestteam());
                holder.time.setText(list.get(i).getTime());
                holder.league.setText(list.get(i).getLeague());
                holder.platform.setText(list.get(i).getPlatform());
                SetTeamlogo.setTeamlogo(holder.hometeamlogo,list.get(i).getHometeam());
                SetTeamlogo.setTeamlogo(holder.guestteamlogo,list.get(i).getGuestteam());

            }

            return view;
        }



    }



    static class ViewHolder{
        TextView time;
        TextView league;
        TextView hometeam;
        TextView guestteam;
        TextView platform;
        ImageView hometeamlogo;
        ImageView guestteamlogo;
        ImageView yuding;
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.allschedule_iv_back:
                finish();
                break;
            case R.id.yingchao:
                finish();
                Intent intent = new Intent(AllscheduleActivity.this,AllscheduleActivity.class);
                intent.putExtra("league","英超");
                startActivity(intent);
                break;
            case R.id.xijia:
                finish();
                intent = new Intent(AllscheduleActivity.this,AllscheduleActivity.class);
                intent.putExtra("league","西甲");
                startActivity(intent);
                break;
            case R.id.dejia:
                finish();
                intent = new Intent(AllscheduleActivity.this,AllscheduleActivity.class);
                intent.putExtra("league","德甲");
                startActivity(intent);
                break;
            case R.id.yijia:
                finish();
                intent = new Intent(AllscheduleActivity.this,AllscheduleActivity.class);
                intent.putExtra("league","意甲");
                startActivity(intent);
                break;
            case R.id.fajia:
                finish();
                intent = new Intent(AllscheduleActivity.this,AllscheduleActivity.class);
                intent.putExtra("league","法甲");
                startActivity(intent);
                break;
        }
    }
}
